require('../../mypublic/css/common.less');
require('../../mypublic/css/index.less');

require('../../public/misc/mediaUpload/css/style.css');
require('../../public/misc/jquery.jqGrid-4.6.0/css/ui.jqgrid.css');
require('../../public/misc/jquery-ui-themes-1.11.2/humanity/jquery-ui.min.css');
require('./index.css');


require("jquery");
var utils = require('@yunke/core/util').default;

require('../../public/misc/jquery.jqGrid-4.6.0/js/i18n/grid.locale-cn');
require('../../public/misc/mediaUpload/js/jquery.form');
require('../../public/misc/mediaUpload/js/myUpload');

require('../../public/js/myservice/side');
require("../../public/js/myservice/authorize");

var angular = require('angular');

angular.module('app', [])
    .controller('mainCtrl', ['$scope', '$http', function ($scope, $http) {
        $scope.data = {
            model: {}
        };

        $scope.action = '/api/index.php?r=official/media/edit&id=' + utils.getUrlPara('id')

        window.postCallback = function (res) {
            if (res.retCode == -1) {
                $('#js_error_msg').html(res.retMsg);
                $scope.$applyAsync();
                alert('保存失败');
            } else {
                alert('保存成功');
                location.href = '/official/media_list/index.html';
            }
        };

        $scope.beforeSubmit = function (e) {
            $('#js_error_msg').html('');
            $scope.key = new Date().getTime();
        };

        $scope.$watch('statusChecked', function () {
            $scope.data.model.status = $scope.statusChecked ? 1 : 0;
        });

        $http.get('/api/index.php?r=/official/media/edit', {
                params: {id: utils.getUrlPara('id')},
                t: new Date().getTime()
            })
            .success(function (res) {
                var data = res.data;
                $scope.data = data;
                $scope.statusChecked = data.model.status == 1;

                oriFunc(data);
            })

    }])

angular.bootstrap(document, ['app']);

function oriFunc(data) {
    var isIE = /msie/i.test(navigator.userAgent) && !window.opera;

    window.fileChange = function (target) {

        var fileSize = 0;
        if (isIE && !target.files) {
            var filePath = target.value;
            var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
            var file = fileSystem.GetFile(filePath);
            fileSize = file.Size;
        } else {
            fileSize = target.files[0].size;
        }
        var size = fileSize / 1024;
        if (size > 8000) {
            alert("附件不能大于8M");
            $('#submit').attr('disabled', true);
        } else {
            $('#submit').attr('disabled', false);
        }
    }
}